//
//  Top.m
//  mcf
//
//  Created by yuike6 yuike6 on 12-1-7.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "Top.h"
#import "header.h"

@implementation Top

@synthesize topApi = top_api_;

- (void) dealloc
{
    YRelease(top_api_);
    [super dealloc];
}


-(NSString *) getApi
{
    return self.topApi.topApiUrl;
}

- (NSData *) getTopApiData:(NSString *) topApi
{
    NSData * result = nil;
    
    if([NSString isNilOrEmpty:topApi])
    {
        return nil;
    }
        
    NSURL * topApiUrl = [NSURL URLWithString:[topApi stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    DebugLog(@"%@", topApiUrl);
    NSURLResponse* response = nil;  
    NSError * error = nil;
    
    
    result = [HTTPUtil dataResultFromGetRequestWithURL:topApiUrl parameters:nil additionalHeaderFields:nil timeoutInterval:10.0f response:&response error:&error];    
        
    return result;
}

- (NSData *) getTopApiData
{
    [self.topApi setSinceTime:[[NSDate date] timeIntervalSince1970]];
    return [self getTopApiData:[self getApi]];
}


- (NSData *) getTopApiDataWithTopApi: (TopApi *) topApiVlaue
{
    return [self getTopApiData:[topApiVlaue topApiUrl]];
}


- (NSString *) wapWangwangUrlString:(NSString *) sidValue
{
    if ([NSString isNilOrEmpty:sidValue]) 
    {
        return nil;
    }
        
    return [_WAP_WANGWANG_URL_ replace:_TOP_SID_PARAM_ with:sidValue];
}

- (NSURL *) wapWangwangUrl:(NSString *) sidValue
{
    if([NSString isNilOrEmpty:sidValue])
    {
        return nil;
    }
    NSString * recvStr = [self wapWangwangUrlString:sidValue];
    if ([NSString isNilOrEmpty:recvStr]) {
        return nil;
    }
    return [NSURL URLWithString:recvStr];
}

+ (BOOL) isTaobaoCatalogByName:(NSString *) catalogName
{
    if ([NSString isNilOrEmpty:catalogName]) {
        return NO;
    }
    
    if ([catalogName isEqualToString:@"taobao"] ||
        [catalogName isEqualToString:@"tmall"]) 
    {
        return TRUE;
    }    
    return NO;
}


+ (NSString *) taobaoProductUrl2Taobao3GUrl:(NSString *) taobaoProductUrl
{
    if ([NSString isNilOrEmpty:taobaoProductUrl]) {
        return nil;
    }
    
    
    if ([taobaoProductUrl startswith:@"http://a.m.t"]) {
        return taobaoProductUrl;
    }
    
    NSArray * theArray = [taobaoProductUrl split:@"&"];
    
    if ([NSArray isNilOrEmpty:theArray]) {
        return nil;
    }
    
    NSString * productId = nil;
    
    for (NSString * paramValue in theArray) 
    {
        if ([paramValue isIncludeSubString:@"id="]) {
            NSArray * paramValues = [paramValue split:@"="];
            
            if ([NSArray isNilOrEmpty:paramValues]) {
                return nil;
            }
            
            if ([paramValues count] != 2) {
                return nil;
            }
            productId = [paramValues objectAtIndex:1];
        }
    }
    
    
    if ([NSString isNilOrEmpty:productId]) {
        return nil;
    }
    
    return [Top taobao3Gurl:productId];
}


+ (NSString *) taobao3GUrl2TaobaoProductUrl:(NSString *) taobao3GUrl
{
    if ([NSString isNilOrEmpty:taobao3GUrl]) {
        return nil;
    }
    
    
    if ([taobao3GUrl startswith:@"http://detail.t"]) {
        return taobao3GUrl;
    }
    
    NSString * name = [YPath fileName:taobao3GUrl];
    NSArray * theArray = [name split:@"."];
    if ([NSArray isNilOrEmpty:theArray]) {
        return nil;
    }
    
    if ([theArray count] != 2) {
        return nil;
    }
    
    NSString * paramValue = [theArray objectAtIndex:0];
    
    if (![paramValue startswith:@"i"]) {
        return nil;
    }
    
    NSRange range;
    range.location = 1;
    range.length = [paramValue length] - 1;
    NSString * productid = [paramValue substringWithRange:range];
    
    if ([NSString isNilOrEmpty:productid]) {
        return nil;
    }
    
    return [Top taobaoProductUrl:productid];
}


+ (NSString *) taobao3Gurl:(NSString *) productid
{
    if ([NSString isNilOrEmpty:productid]) {
        return nil;
    }
    
    
    NSString * result = TAOBAO_3G_URL_TEMPLATE;
    result = [result replace:_PARAM_TAOBAO_PRODUCTID_ with:productid];    
    return result; 
}


+ (NSString *) taobaoProductUrl:(NSString *) productid
{
    if ([NSString isNilOrEmpty:productid]) {
        return nil;
    }
    
    NSString * result = TAOBAO_PRODUCT_URL_TEMPLATE;
    result = [result replace:_PARAM_TAOBAO_PRODUCTID_ with:productid];
    
    return result;
}


+ (NSString *) taobaoProductUrl2Taobao3GUrlAndTtid:(NSString *) taobaoProductUrl
{
    NSString * result = [Top taobaoProductUrl2Taobao3GUrl:taobaoProductUrl];
    
    if (![NSString isNilOrEmpty:result]) {
        result = [result stringByAppendingFormat:@"&ttid=%@", _TTID_FOR_YMALL_];
    }
    
    return result;
}

+ (NSString *) taobao3GUrl2TaobaoProductUrlAndTtid:(NSString *) taobao3GUrl
{
    NSString * result = [Top taobao3GUrl2TaobaoProductUrl:taobao3GUrl];
    
    if (![NSString isNilOrEmpty:result]) {
        result = [result stringByAppendingFormat:@"&ttid=%@", _TTID_FOR_YMALL_];
    }
    
    return result;
}

+ (NSString *) taobaoProductUrlAndTtid:(NSString *) productid
{
    NSString * result = [Top taobaoProductUrl:productid];
    
    if (![NSString isNilOrEmpty:result]) {
        result = [result stringByAppendingFormat:@"&ttid=%@", _TTID_FOR_YMALL_];
    }
    
    return result;
}







@end
